<?php

namespace App\Http\Controllers;


use App\Helpers\Tools;
use App\Models\PrizeDraw;
use Illuminate\Support\Facades\Redis;

class AnswerController extends ApiController
{
    /**
     * 中奖登记
     * @param int $activities_id 活动id
     * @param int $adverti_id 奖券id
     * @param int $nickname 姓名
     * @param string $mobile 电话
     * @param string $address 地址
     *
     * @author 日隆
     * @return $id
     */
    public function apply()
    {
        // 获取数组
        $data = request()->only('activities_id', 'adverti_id', 'nickname', 'mobile', 'address');

        // 判断消耗是否已满
        $places = $this->applyPlaces($data['adverti_id']);

        if ($places) {
            $prizeDraw = new PrizeDraw();
            $prizeDraw->fill($data);
            $prizeDraw->ip = $this->getIp();
            $res = $prizeDraw->save();

            if ($res) {
                return Tools::success();
            } else {
                return Tools::error('入库失败');
            }
        } else {
            return Tools::error('本奖品已送完，下次参与还有机会哦！');
        }
    }

    /**
     * 获取单条奖品信息
     * @param int $adverti_id 奖券id
     *
     * @author 日隆
     * @return $info
     */
    protected function advertiInfo($id)
    {
        $time = time();
        if ($time - Redis::hget("adverti:info:$id", 'updateTime') > 20) {
            $info = Adverti::find($id);

            Redis::hset("adverti:info:$id", 'updateTime', $time);
            Redis::hset("adverti:info:$id", 'info', serialize($info));
            Redis::hset("adverti:info:$id", 'budget', $info->day_budget);
        } else {
            $info = unserialize(Redis::hget("adverti:info:$id", 'info'));
        }

        return $info;
    }

    // 抽奖按钮
    public function draw()
    {
        $position = $this->digui();

        if (!$position) {
            $position = 2;
        }

        return Tools::setData([
            'position' => $position
        ]);
    }

    // 递归抽奖
    protected function digui() {
        $position = rand(2, 5);
        $res = $this->places($position);
        if ($res) {
            return $position;
        }

        $this->digui();
    }

    /**
     * 消耗查看
     * @param int $adverti_id 奖券id
     *
     * @author 日隆
     * @return $adverti
     */
    protected function places($id)
    {
        // 先添加消耗
        $day = date('Y-m-d');
        $useBudget = Redis::hget("adverti:info:$id", "$day:useBudget");

        $budgets = [
            2 => 10000000, // 优惠券
            3 => 0, // 签名
            4 => 0, // 欢乐谷
            5 => 34, // 抱枕
        ];

        if ($useBudget < $budgets[$id]) {
            Redis::hincrby("adverti:info:$id", "$day:useBudget", 1);
            return true;
        } else {
            return false;
        }
    }

    // 填写信息的
    protected function applyPlaces($id)
    {
        // 先添加消耗
        $day = date('Y-m-d');
        $useBudget = Redis::hget("adverti:info:$id", "$day:useBudget:apply");

        $budgets = [
            2 => 10000000, // 优惠券
            3 => 0, // 签名
            4 => 0, // 欢乐谷
            5 => 34, // 抱枕
            7 => 10000000, // 优惠券
        ];

        if ($useBudget < $budgets[$id]) {
            Redis::hincrby("adverti:info:$id", "$day:useBudget:apply", 1);
            return true;
        } else {
            return false;
        }
    }

    public function excel() {

    }

    /**
     * 获取用户真实ip
     *
     * @author 日隆
     *
     */
    protected function getIp()
    {
        $ip = !empty($_SERVER['HTTP_CLIENT_IP']) ? $_SERVER['HTTP_CLIENT_IP'] : (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : (!empty($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'unknown'));
        return $ip;
    }
}
